using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dune.Business.Domain.Forum;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Web.Controls.Grid;
using Edge.Common.Web.DataSourceAdapters;
using Dune.Web.AppCode;
using SortDirection=Edge.Common.SortDirection;

namespace Dune.Web
{
    public partial class Themes : BaseDunePage
    {
        [QueryParameter("id", IsRequired = false)]
        public int themeID;

        private FilteredDataSourceFromSession<IThemeManager, Theme, ThemeFilter> dataSource;
        private Theme currentTheme;
        //private BaseDataSource dataSource;

        private Theme CurrentTheme
        {
            get { return currentTheme; }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            WebParameterAttribute.SetValues(this, Request);
            currentTheme = null;
            if (themeID != 0)
            {
                currentTheme = Locator.GetService<IThemeManager>().Load(themeID);
                //lblParentTheme.Text = currentTheme.Name;
            }
            if (currentTheme != null && currentTheme.ParentTheme != null && currentTheme.ParentTheme.ParentTheme != null)
            {
                Response.Redirect("Posts.aspx?id="+currentTheme.Id.ToString());
                
            }
            else
            {
                amMenu.DataSourse = Locator.GetService<IThemeManager>().ConvertToNavigation(Locator.GetService<IThemeManager>().GetCategories(null),currentTheme);
                //gridForum.DataBind();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            CurrentPageType = PageType.Forum;
            base.OnInit(e);
            WebParameterAttribute.SetValues(this, Request);
            //dataSource = new FilteredDataSourceFromSession<IThemeManager, Theme, ThemeFilter>(gridForum, GetTemplateFilter());
            //GridHandlers.Setup(gridForum, "Id", SortDirection.Ascending);
            //btnAdd.NavigateUrl = "ThemeDetails.aspx?action=new&id=" + themeID.ToString();
        }

        protected ThemeFilter GetTemplateFilter()
        {
            ThemeFilter template = new ThemeFilter();
            WebParameterAttribute.SetValues(this, Request);
            if (themeID != 0)
                template.Parent = Locator.GetService<IThemeManager>().Load(themeID);
            else
                template.Parent = null;
            //template.Keyword = "";
            return template;
        }

        protected void gridForum_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //            if (e.Row.RowType == DataControlRowType.DataRow)
            //            {
            //                Person item = (Person)e.Row.DataItem;
            //                HyperLink hlEdit = (HyperLink)e.Row.FindControl("hlEdit");
            //                hlEdit.NavigateUrl = "PersonDetails.aspx?action=edit&id=" + item.Id.ToString();
            //                HyperLink hlDelete = (HyperLink)e.Row.FindControl("hlDelete");
            //                hlDelete.NavigateUrl = "PersonDetails.aspx?action=delete&id=" + item.Id.ToString();
            //            }
        }
    }
}
